package team.archai.nexus.boot.web.handlers;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import team.archai.nexus.boot.auth.exception.ExpiredException;
import team.archai.nexus.boot.constant.HttpEnum;
import team.archai.nexus.boot.tool.HttpUtil;
import team.archai.nexus.boot.web.response.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户登录失败处理类
 *
 * @author Mr.Longyx
 * @date 2020年01月11日 9:50
 */
@Component
@ConditionalOnProperty(name = "archai-nexus.auth.type", havingValue = "security")
public class AuthFailureHandler implements AuthenticationFailureHandler {
    /**
     * 登录失败返回结果
     *
     * @Author Longyx
     * @CreateTime 2019/10/3 9:12
     */
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws ExpiredException {
        HttpUtil.responseJson(response, ResultUtil.out(HttpEnum.AUTH_FAILED));
    }
}
